package com.zhangyj.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zhangyj.util.Tools;

public class UserContext {
	public static final String key = "usercontext";

	static List<String[]> userTypes = null;

	private int userid;
	private String nickName;
	private String lgnName;
	private String datePatten="yyyy-MM-dd";
	private String dateTimePatten="yyyy-MM-dd HH:mm:ss";
	private int entid;
	public UserContext(int userid, String lgnName,String nickName,int entid) {
		this.userid = userid;
		this.lgnName = lgnName;
		this.nickName = nickName;
		this.entid = entid;
	}

	public static boolean isAnonymity(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		if (session == null)
			return true;
		Object context = session.getAttribute(key);
		if (context == null)
			return true;
		return false;
	}

	public static UserContext getUserContext(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		if (session == null)
			return null;
		UserContext context = (UserContext) session.getAttribute(key);
		return context;
	}
	
	public static void setUserContext(HttpServletRequest request,UserContext userContext) {
		HttpSession session = request.getSession(true);
		session.setAttribute(key, userContext);
	}

	public int getUserid() {
		return userid;
	}

	public String getLgnName() {
		return lgnName;
	}
	public String getNickName() {
		return nickName;
	}

	public String getDatePatten() {
		return datePatten;
	}

	public String getDateTimePatten() {
		return dateTimePatten;
	}
	
	public String toJSON() throws IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("datePatten", this.getDatePatten());
		map.put("dateTimePatten", this.getDateTimePatten());
		map.put("userName", this.getNickName());
		return Tools.object2jsonStr(map);
	}

	public int getEntid() {
		return entid;
	}
}
